package com.insurance.controller;

import com.insurance.domain.ResponseResult;
import com.insurance.domain.entity.Category;
import com.insurance.domain.enums.AppHttpCodeEnum;
import com.insurance.domain.vo.CategoryPageVo;
import com.insurance.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author qcjstudy
 * @create 2023-04-22 13:08
 */
@RestController
@RequestMapping("/api/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    private CategoryPageVo categoryPageVo;

    @PostMapping("/list")
    public ResponseResult list(@RequestBody CategoryPageVo categoryPageVo){
        this.categoryPageVo = categoryPageVo;
        return categoryService.listByPage(categoryPageVo);
    }

    @PostMapping()
    public ResponseResult add(@RequestBody Category category){
        return categoryService.addCategory(category);
    }

    @PutMapping("/update")
    public ResponseResult update(@RequestBody Category category){
        return categoryService.updateCategory(category);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable Long id){
        if(categoryService.removeById(id)){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_FAILED);
    }
}
